package cn.jn.lhm.uc.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.jn.lhm.service.config.exception.GlobalException;
import cn.jn.lhm.uc.constant.UcConstant;
import cn.jn.lhm.uc.entity.UcenterMember;
import cn.jn.lhm.uc.entity.vo.RegisterVo;
import cn.jn.lhm.uc.mapper.UcMapper;
import cn.jn.lhm.uc.service.UcService;
import cn.jn.lhm.utils.JwtUtils;
import cn.jn.lhm.utils.MD5;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author LHM
 * @Tips 热爱每一天，奋斗每一天
 * @date 2020/6/10 - 21:26
 */
@Service
@Slf4j
public class UcServiceImpl implements UcService {

    @Resource
    private RedisTemplate<String, String> template;

    @Resource
    private UcMapper ucMapper;

    @Override
    public String loginUser(UcenterMember member) {
        //检验参数合法性
        String mobile = member.getMobile();
        String password = member.getPassword();
        if (StrUtil.hasEmpty(mobile, password)) {
            throw new GlobalException(20001, UcConstant.USER_LOGIN_ERROR_MISS_INFO);
        }
        //查询手机还是否存在
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        UcenterMember originalMember = ucMapper.selectOne(wrapper);
        if (originalMember == null) {
            throw new GlobalException(20001, UcConstant.USER_LOGIN_ERROR_USER_NOT_EXIT);
        }
        //比较密码
        if (!MD5.encrypt(password).equals(originalMember.getPassword())) {
            throw new GlobalException(20001, UcConstant.USER_LOGIN_ERROR_PASSWORD);
        }
        //检查用户的合法性
        if (originalMember.getIsDisabled()) {
            throw new GlobalException(20001, UcConstant.USER_LOGIN_ERROR_ACCOUNT_EXCEPTION);
        }
        //登录成功，生成token,返回对应的token
        //根据用户的id和nickname生成对应的token
        return JwtUtils.getJwtToken(originalMember.getId(), originalMember.getNickname());
    }

    @Override
    public void registerUser(RegisterVo registerVo) {
        //获取所有的属性
        String code = registerVo.getCode();
        String mobile = registerVo.getMobile();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();
        if (StrUtil.hasEmpty(mobile, password, code, nickname)) {
            throw new GlobalException(20001, UcConstant.USER_LOGIN_ERROR_MISS_INFO);
        }
        String redisCode = template.opsForValue().get(mobile);
        log.info("获取的redisCode为 :" + redisCode + "\t输入的code为 ：" + code);
        //判断验证码
        if (!code.equals(JSON.parseObject(redisCode, String.class))) {
            throw new GlobalException(20001, UcConstant.USER_REGISTER_ERROR_CODE_ERROR);
        }
        //判断手机号是否重复
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        if (ucMapper.selectCount(wrapper) > 0) {
            throw new GlobalException(20001, UcConstant.USER_REGISTER_ERROR_MOBILE_EXIT);
        }
        //传入到数据库，加密密码
        registerVo.setPassword(MD5.encrypt(password));
        UcenterMember member = new UcenterMember();
        BeanUtils.copyProperties(registerVo, member);
        member.setIsDisabled(false);
        ucMapper.insert(member);
    }

    @Override
    public UcenterMember getUserInfo(String userId) {
        if (StrUtil.hasEmpty(userId)){
            throw new GlobalException(20001,  UcConstant.USER_LOGIN_ERROR_MISS_TOKEN);
        }
        return ucMapper.selectById(userId);
    }

    @Override
    public void saveUser(UcenterMember member) {
        ucMapper.insert(member);
    }

    @Override
    public UcenterMember getUserInfoByWXOpenid(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid", openid);
        return ucMapper.selectOne(wrapper);
    }

    @Override
    public Integer countRegister(String day) {
        return ucMapper.countNumber(day);
    }
}
